<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="closeEvent"
  >
    <!-- 表单 -->
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="deptRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" style="width:80%" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" style="width:80%" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="deptForm.managerId" style="width:80%" size="mini" placeholder="请选择负责人">
          <el-option
            v-for="item in manageList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" :rows="4" style="width:80%" size="mini" placeholder="请输入部门介绍" />
      </el-form-item>
      <el-form-item>
        <el-row style="width:80%" type="flex" justify="center">
          <el-button size="mini" type="success" @click="addList">确定</el-button>
          <el-button size="mini" type="danger" @click="closeEvent">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDeptsApi, getManagerAPI, addDeptAPI, getDepartmentDetailAPI, upDepartmentAPI } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      manageList: [],
      deptForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let deptList = await getDeptsApi()
              // 区分新增跟编辑
              if (this.deptForm.id) {
                // 筛选出去本身之外的数据
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              const isShow = deptList.some(item => item.name === value)
              if (isShow) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              let deptList = await getDeptsApi()
              // 区分新增跟编辑
              if (this.deptForm.id) {
                // 筛选出去本身之外的数据
                deptList = deptList.filter(item => item.id !== this.deptForm.id)
              }
              const isShow = deptList.some(item => item.code === value)
              if (isShow) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.deptForm.id ? '编辑部门' : '添加子部门'
    }
  },
  created() {
    this.getManageList()
  },
  methods: {
    // 关闭弹窗，重置数据
    closeEvent() {
      this.$emit('update:showDialog', false)
      this.deptForm = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      this.$refs.deptForm.resetFields()
    },
    async getManageList() {
      this.manageList = await getManagerAPI()
    },
    // 点击确定
    addList() {
      this.deptForm.pid = this.pid
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          if (this.deptForm.id) {
            await upDepartmentAPI(this.deptForm)
            // 提示用户
            this.$message.success('修改成功')
          } else {
            await addDeptAPI(this.deptForm)
            // 提示用户
            this.$message.success('添加成功')
          }
          // 提示重新获取
          this.$emit('addDept')
          this.closeEvent()
        }
      })
    },
    // 编辑按钮
    async edit() {
      this.deptForm = await getDepartmentDetailAPI(this.pid)
    }
  }
}
</script>

<style>

</style>
